﻿using ACC.Business;
using ACC.Business.Process;
using ACC.Business.Stn;
using ACC.Data;
using ACC.Data.Comm;
using ACC.Data.Line;
using ACC.Device;
using ACC.Device.Client;
using ACC.Device.PLC.Siemens;
using ACC.Interface.Extensions;
using ACC.LineDevice.KukaPack;
using System;
using System.Text;

namespace Script
{
    /// <summary>
    /// 贴侧绝缘板
    /// </summary>
    public class L1M13111 : StnProcessKukaPack
    {
        public L1M13111(StnRunningData data)
            : base(data)
        {
        }
        protected override ACCDevice CreateDevice()
        {

            ACCDevice dev = base.CreateDevice();
            if (kukaSiemensDev != null)
            {
                kukaSiemensDev.DB190 = 800;
                kukaSiemensDev.DB190_Offset = 7000;
                kukaSiemensDev.DB192 = 80;
                kukaSiemensDev.DB192_PLC_Offset = 7066;
                kukaSiemensDev.DB193 = 800;
                kukaSiemensDev.DB193_PLC_Offset = 7034;
                kukaSiemensDev.DB193_ACC_Offset = 7068;
                kukaSiemensDev.DB194 = 800;
                kukaSiemensDev.DB194_PLC_Offset = 7034;
                kukaSiemensDev.DB194_PLC_Data_Offset = 7078;
            }

            return dev;
        }

        public override ACCCode ReadUnloadParam(ACCDevice dev, out ACCDevice.UnloadParam param)
        {
            materialTypeCount = -1;
            ACCCode rs = base.ReadUnloadParam(dev, out param);
            param.UnitSN = GetUnLoadPallet();
            if (rs == ACCCode.ACCOK && !string.IsNullOrEmpty(param.UnitSN))
            {
                if (!(dev is ClientDevice))
                {
                    // 通过Pallet号获取内部码 
                    string[] v = ACCExt.GetPallet(this, param.UnitSN);
                    if (v != null && v.Length > 0)
                    {
                        param.UnitSN = v[0];                        
                    }
                }
            }

            WIPUnit unit = UnitDataProcess.GetWIPUnit(_lineData, param.UnitSN);
            if (unit != null)
            {
                rs = AutoChangePartNo(unit.PartNo, ref param);
            }

            // 因为本站一次可load两个模组、所以第一个load件的物料信息会被第二个load件的物料信息覆盖，
            // 导致第一个件unload时扣料发生异常。 所以本站在unload时再进行一次扫料处理
            if (rs == ACCCode.ACCOK)
            {
                materialTypeCount = GetMaterialCount();
                if (materialTypeCount > 0)
                {
                    rs = HandleScanMaterial(dev, string.Empty/*GetOrderNo(param.UnitSN)/*param.UnitSN*/);
                }
            }
            return rs;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public string GetUnLoadPallet()
        {
            string _pallet = "";
            try
            {
                byte[] data = new byte[32];
                byte[] temp = new byte[30];
                ((SiemensDevice)_Device).ReadDBBytes(((SiemensDevice)_Device).DB194, kukaSiemensDev.DB194_PLC_Offset + 38, data);
                Array.Copy(data, 2, temp, 0, temp.Length);
                _pallet = Encoding.ASCII.GetString(temp);
                if (_pallet != null && _pallet.Length > data[1])
                {
                    _pallet = _pallet.Substring(0, data[1]);
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex.StackTrace);
            }
            return _pallet;
        }

    }
}